package org.tmad.ocms.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.apache.log4j.Logger;
import org.tmad.ocms.exceptions.ApplicationException;
import org.tmad.ocms.log.LogFactory;
import org.tmad.ocms.model.Case;
import org.tmad.ocms.model.Volunteer;
import org.tmad.ocms.service.CaseService;
import org.tmad.ocms.service.MasterDataService;
import org.tmad.ocms.service.RegistrationService;

/**
 * Servlet implementation class CaseRegistrationServlet
 */

@SuppressWarnings("unused")
public class CaseRegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Integer IN_PROGRESS = 3;
	private static Logger logger = LogFactory.getLogger();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CaseRegistrationServlet() {
        super();
       
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setAttribute("caseTypes", MasterDataService.getCaseCategories());
		request.setAttribute("helpTypes", MasterDataService.getHelpCategories());
		request.setAttribute("states", MasterDataService.getStates());
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher("/new_case.jsp");
		dispatcher.forward(request,response);
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
		String caseType= request.getParameter("case_category_id");
		String helpType = request.getParameter("help_category_id");
		String summary = request.getParameter("case_summary");
		
		String description = request.getParameter("case_description");
		String needyName = request.getParameter("person_name");
		String city = request.getParameter("city");
		String address=request.getParameter("address");
		String state = request.getParameter("state_id");
		String postalCode = request.getParameter("postal_code");
		String mobile = request.getParameter("contact_mobile");
		String contactPhone = request.getParameter("contact_phone");
		String emailId = request.getParameter("contact_email");
		Integer statusId =IN_PROGRESS;//defaulting to In progress.
		String userName= request.getParameter("userName");
		Case user =new Case(caseType,helpType,summary,description,needyName,city,state,postalCode,mobile,contactPhone,emailId,statusId,userName);
		try {
			CaseService service = new CaseService();
			service.caseUser(user);
			HttpSession session = request.getSession();
			setMenuItems(session);
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/home.jsp");
			dispatcher.forward(request, response);
		} catch (Exception e) {
			logger.error("Error while registration", e);
			// show error on page
			
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/new_case.jsp");
			dispatcher.forward(request, response);

		}
	}
	private void setMenuItems(HttpSession session) {
		Map<String, String> roleBasedMenu = new LinkedHashMap<String,String>();
		roleBasedMenu.put("index.jsp","Home Page");
		roleBasedMenu.put("newCase", "Submit new case");
		roleBasedMenu.put("casereg.jsp", "casedetails");
		roleBasedMenu.put("userlogin.jsp", "your profile");
		session.setAttribute("menu", roleBasedMenu);
	}

}
